package com.qf.service.impl;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 路由策略
 * @author zhaojian
 */
@Service(value = "router")
@Slf4j
public class RouterFilterServiceImpl implements FilterChainService {


    @Autowired
    private RabbitTemplate rabbitTemplate;
  /*  @Autowired
    private RabbitAdmin rabbitAdmin;*/
    @Autowired
    private AmqpAdmin amqpAdmin;

    /**
     * 路由   :  发送消息RabbitMQ   sms_send_gateway_0：未知   sms_send_gateway_1：移动
     * **路由 ：**从客户通道表中选择所配的通道下发
     *
     * - 如果找不到通道把发送日志存到Elasticsearch
     * - 如果同时是HTTP的发送方式就模拟出状态报告推送给客户
     * @param submit
     * @return
     */
    @Override
    public boolean strategy(StandardSubmit submit) {
        Integer operatorId = submit.getOperatorId();//运营商的类型
        //1:动态的创建队列  使用默认交换机 也不绑定
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + operatorId;
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());
        //移动
        //发送RabbitMQ的消息
        //待发送短信网关队列 + 网关ID号   ---  根据手机号码情况不同,发送给不用的网关模块处理
    /*    2: public final static String TOPIC_SMS_GATEWAY = "sms_send_gateway_";*/
        //  sms_send_gateway_0 未知   sms_send_gateway_1 移动 sms_send_gateway_2 电信
        rabbitTemplate.convertAndSend(queueName,submit);
        return true;
    }
}